package com.example.restaurant.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.restaurant.common.Log;
import com.example.restaurant.common.RestaurantException;
import com.example.restaurant.common.allenum.DishTypeStatus;
import com.example.restaurant.dao.DishMapper;
import com.example.restaurant.model.Dish;
import com.example.restaurant.model.DishType;
import com.example.restaurant.dao.DishTypeMapper;
import com.example.restaurant.service.DishTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.restaurant.service.param.AddDishTypeParam;
import com.example.restaurant.service.param.UpdateDishTypeParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜类表 服务实现类
 * </p>
 *
 * @author x
 * @since 2022-09-29
 */
@Service
@Slf4j
public class DishTypeServiceImpl extends ServiceImpl<DishTypeMapper, DishType> implements DishTypeService {

    @Resource
    private DishTypeMapper dishTypeMapper;
    @Resource
    private DishMapper dishMapper;

    @Override
    //查询所有菜类
    @Log
    public Page<DishType> getDishType(int userId, int current, int size) {
        log.info("{},{},{}", userId, current, size);
        log.info("查询数据库");
        QueryWrapper<DishType> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("user_id", userId);
        Page<DishType> dishTypePage = dishTypeMapper.selectPage(new Page<>(current, size), queryWrapper);
        if (dishTypePage == null || dishTypePage.getTotal() <= 0) {
            throw new RestaurantException("数据库没查到数据");
        }
        return dishTypePage;
    }


    @Override
    //修改菜类
    @Transactional
    @Log
    public boolean modifyDishType(UpdateDishTypeParam updateDishTypeParam) {
        log.info("{}", updateDishTypeParam.toString());
        DishType dishType = BeanUtil.copyProperties(updateDishTypeParam, DishType.class);
        QueryWrapper<DishType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(updateDishTypeParam.getId() > 0, "id", updateDishTypeParam.getId());
        int update = dishTypeMapper.update(dishType, queryWrapper);
        if (update < 1) {
            throw new RestaurantException("修改菜类失败");
        }
        return true;
    }

    @Override
    //新增菜类
    @Transactional
    @Log
    public boolean addDishType(AddDishTypeParam addDishTypeParam) {
        log.info("{}",addDishTypeParam.toString());
        DishType dishType = BeanUtil.copyProperties(addDishTypeParam, DishType.class);
        dishType.setStatus(DishTypeStatus.USE.toString());
        int insert = dishTypeMapper.insert(dishType);
        if(insert<1){
            throw new RestaurantException("添加菜类失败");
        }
        return true;
    }


    @Override
    //删除菜类 根据菜类id 先查菜品表下是否有此菜类，有则不能删除
    @Transactional
    public boolean delDishType(int dishTypeId) {
        if (dishTypeId < 1) {
            throw new RestaurantException("参数错误");
        }
        QueryWrapper<Dish> dishQueryWrapper = new QueryWrapper<>();
        dishQueryWrapper.eq("dish_type_id", dishTypeId);
        List<Dish> dishes = dishMapper.selectList(dishQueryWrapper);
        log.info("{}", dishes.toString());
        if (dishes.size() != 0) {
            throw new RestaurantException("该菜类下有菜品，不能删除");
        }
        int i = dishTypeMapper.deleteById(dishTypeId);
        if (i < 1) {
            throw new RestaurantException("删除失败");
        }
        return true;
    }

    //通过菜类Id获取菜类信息
    @Override
    public List<DishType> getDishTypeById(int storeId) {
        //获取门店下所有菜类ID
        List<Integer> DishTypeIds = getDishTypeIdByStoreId(storeId);
        ArrayList<DishType> dishTypes = new ArrayList<>();
        for (Integer dti: DishTypeIds
             ) {
            QueryWrapper<DishType> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id",dti);
            queryWrapper.eq("status","USE");
            DishType dishType = dishTypeMapper.selectOne(queryWrapper);
            if(dishType==null||"".equals(dishType)){
                throw new RestaurantException("系统有误");
            }else{
                dishTypes.add(dishType);
            }
        }
        return dishTypes;
    }

    //根据门店id和模糊类型名字，模糊查询
    @Override
    public Page<DishType> selDishTypeByName(int storeId, String dishTypeName, int current, int size) {
        QueryWrapper<DishType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("store_id",storeId);
        List<DishType> dishTypes = dishTypeMapper.selectList(queryWrapper);
        if(dishTypes.size()==0){
            throw new RestaurantException("该店还没有菜品类型，请去添加");
        }
        queryWrapper.like(dishTypeName!=null,"dish_type_name",dishTypeName);
        Page<DishType> dishTypePagePage = dishTypeMapper.selectPage(new Page<>(current, size), queryWrapper);

        return dishTypePagePage;
    }



    //通过门店ID查询菜类ID
    @Override
    public List<Integer> getDishTypeIdByStoreId(int storeId) {
        QueryWrapper<Dish> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("store_id", storeId);
        List<Dish> dishes = dishMapper.selectList(queryWrapper);
        if (dishes.size() > 0) {
            ArrayList<Integer> ai = new ArrayList<>();
            for (Dish d : dishes) {
                ai.add(d.getDishTypeId());
            }
            //去重
            List<Integer> newAi = delRepeat(ai);
            return newAi;
        } else {
            throw new RestaurantException("该门店还未录入菜品");
        }
    }



    //List去重
    public List<Integer> delRepeat(List<Integer> ai) {
        List<Integer> newList = new ArrayList<>(ai.size());
        ai.forEach(i -> {
            if (!newList.contains(i)) { // 如果新集合中不存在则插入
                newList.add(i);
            }
        });
        return newList;
    }
}
